﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Functions;
using SchoolProject2012.Areas.Eleve.Models;
using System.Data;

namespace SchoolProject2012.Areas.Eleve.Controllers
{
    public class CompteController : Controller
    {
        private EleveSchoolProjectBddEntities db = new EleveSchoolProjectBddEntities();
        //
        // GET: /Eleve/Compte/
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                return View();
            }
            catch (Exception)
            {
                return View("error");
            }
        }

        //
        // GET: /Eleve/Compte/Details/
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult Details(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                var user = db.ELEVE.Single(x => x.LOGINPERS == login);
                return View(user);
            }
            catch (Exception)
            {
                return View("error");
            }
        }

        //
        // GET: /Eleve/Compte/Edit/
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        public ActionResult Edit(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                string login = Session["login"].ToString();
                ELEVE eleve = db.ELEVE.Single(x => x.LOGINPERS == login);
                if (eleve == null)
                {
                    return HttpNotFound();
                }
                return View(eleve);
            }
            catch (Exception)
            {
                return View("error");
            }
            
        }

        //
        // POST: /Eleve/Compte/Edit/
        [Authorize(Roles = "Admin, Eleve, Delegue, BDE")]
        [HttpPost]
        public ActionResult Edit(string langue, ELEVE eleveToUpdate)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            ViewBag.langue = langue;
            try
            {
                db.ELEVE.Attach(eleveToUpdate);
                db.ObjectStateManager.ChangeObjectState(eleveToUpdate, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View("error");
            }
        }
    }
}
